<?php


namespace app\api\controller;


use app\common\controller\CommonBase;
use app\common\model\Lane;
use app\common\model\Parklog;
use app\common\model\TicketNo;
use etpcar\EtpCar;

class Carnotice extends CommonBase
{
    public function wait_out($param, $parkinfo)
    {
        $option = [
            'key' => $parkinfo['parkkey'],
            'parkno' => $parkinfo['parkno'],
        ];
        $params = [
            'carno' => $param['key1value'],
            'outtime' => $param['key2value']
        ];
        $Carapi = new EtpCar($option);
        $res = $Carapi->get_wait_out($params);
        if ($res['ReMsg']['ErrNo'] == "0000") {
            $laneinfo = Lane::where(['parkid' => $parkinfo['id'], 'lane_ip' => $res['OutMachIp']])->find();
            $last_log_id = $this->getcarlastid($parkinfo['id'], $param['key1value']);
            //保存出场信息
            if ($laneinfo) {
                $s_data['outid'] = $laneinfo['id'];
                $s_data['outno'] = $laneinfo['lane_num'];
            }
            $Parklog = new Parklog();
            $Parklog->save($s_data, ['id' => $last_log_id]);
        }
        return $res;
    }

    public function incar($param, $parkinfo)
    {
        try {
            $option = [
                'key' => $parkinfo['parkkey'],
                'parkno' => $parkinfo['parkno'],
            ];
            $params = [
                'carno' => $param['key1value'],
                'intime' => $param['key2value']
            ];

            $Carapi = new EtpCar($option);
            $res = $Carapi->get_in_car($params);
            if ($res['ReMsg']['ErrNo'] == "0000") {
                $CarPay = new CarPay();
                $re_param = $CarPay->inpay($res, $parkinfo);
                //判断是否开启无感支付
                if ($re_param['return_code'] == "SUCCESS") {
                    $s_param['is_wx_wg'] = "Y";
                }
                $laneinfo = Lane::where(['parkid' => $parkinfo['id'], 'lane_ip' => $res['InMachIp']])->find();
                if ($laneinfo) {
                    $s_param['inid'] = $laneinfo['id'];
                    $s_param['inno'] = $laneinfo['lane_num'];
                }
                //保存入场信息
                $s_param['sysid'] = $parkinfo['sysid'];
                $s_param['parkid'] = $parkinfo['id'];
                $s_param['intime'] = $res['InTime'];
                $s_param['carno'] = $res['CarNo'];
                $s_param['intime'] = date('Y-m-d H:i:s', strtotime($res['InTime']));
                $s_param['ininfo'] = json_encode($res);
                $s_param['inlane'] = $res['InLane'];
                $Parklog = new Parklog();
                $Parklog->save($s_param);
                $this->upparkcar($s_param['carno'], $parkinfo['id'], $Parklog->getLastInsID());
            } else {
                $re_param['return_code'] = "FAIL";
                $re_param['return_msg'] = $res['ReMsg']['ErrMsg'];
            }
        } catch (\Exception $e) {
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = $e->getMessage();
            file_put_contents('/data2/html/apis/3.txt', "[" . date('Y-m-d H:i:s') . "]" . "记录入场debug:" . json_encode($re_param) . PHP_EOL, 8);

        }
        return $re_param;


    }

    public function outcar($param, $parkinfo)
    {
        {
            $option = [
                'key' => $parkinfo['parkkey'],
                'parkno' => $parkinfo['parkno'],
            ];
            $params = [
                'carno' => $param['key1value'],
                'outtime' => $param['key2value']
            ];
            $last_log_id = $this->getcarlastid($parkinfo['id'], $param['key1value']);
            //获取车辆出场使用卡券信息

            $Carapi = new EtpCar($option);
            $res = $Carapi->get_out_car($params);
            $ticket_data=array(
                'carno'=>$param['key1value'],
                'StartTime'=>date('YmdHis',strtotime($res['InTime'])),
                'EndTime'=>date('YmdHis',strtotime($res['OutTime']))
            );
            $EptCar = new EtpCar($option);
            $ticket_res = $EptCar->get_pay_list($ticket_data);
            if ($ticket_res['ReMsg']['ErrNo'] == "0000") {
                $ticketinfo = [];
                if (isset($ticket_res['PayList']['PayInfo'])) {
                    $payinfo = $ticket_res['PayList']['PayInfo'];
                    foreach ($payinfo as $k => $v) {
                        if ($v['TicketNo']) {
                            $ticketinfo[$k] = $v['TicketNo'];
                        }
                    }
                }
            }
            if(isset($ticketinfo)){
                $map['ticket_no']=array('in',$ticketinfo);
                $datas['status']="Y";
                $datas['use_time']=date('Y-m-d H:i:s');
                $datas['use_parklog_id']=$last_log_id;
                TicketNo::where($map)->update($datas);
            }

            $s_data['ticket_no']=json_encode($ticketinfo);
            $s_data['status'] = "Y";
            $s_data['outtime'] = $res['OutTime'];
            //获取出场设备信息
            $laneinfo = Lane::where(['parkid' => $parkinfo['id'], 'lane_ip' => $res['OutMachIp']])->find();
            //保存出场信息
            if ($laneinfo) {
                $s_data['outid'] = $laneinfo['id'];
                $s_data['outno'] = $laneinfo['lane_num'];
            }
            if ($res['InPic']) {
                $s_data['inpic'] = "http://www.etpcar.com" . $res['InPic'];
            }
            if ($res['OutPic']) {
                $s_data['outpic'] = "http://www.etpcar.com" . $res['OutPic'];
            }
            $s_data['outtime'] = $res['OutTime'];
            $s_data['outlane'] = $res['OutLane'];
            $Parklog = new Parklog();
            $Parklog->save($s_data, ['id' => $last_log_id]);
            return $res;
        }
    }
}